﻿using Modbus.Data;
using Modbus.Device;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;

namespace ModbusServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Test();
        }


        private async void Test()
        {
            byte slaveId = 1;
            int port = 8899;
            IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });

            // create and start the TCP slave
            TcpListener slaveTcpListener = new TcpListener(address, port);
            slaveTcpListener.Start();

            ModbusSlave slave = ModbusTcpSlave.CreateTcp(slaveId, slaveTcpListener);
            slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
            await slave.ListenAsync();
        }
    }
}
